package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberDemandBinding;
import com.sgb.member.ui.fragment.PromotionInfoFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 促销信息
 */
public class PromotionInfoActivity extends AppActivity {

    private ActivityMemberDemandBinding mBinding;
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        Intent intent = new Intent(context, PromotionInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_demand);
        initSearchView();

        List<String> titles = new ArrayList<>();
        CommonPopEntity popEntity = new CommonPopEntity("", "不限");
        titles.add(popEntity.getTypeName());
        mFragments = new ArrayList<>();
        mFragments.add(PromotionInfoFragment.newInstance(0, popEntity));
        SlidingFragmentPagerAdapter adapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles.toArray(new String[]{}));
        mBinding.vp.setOffscreenPageLimit(titles.size());
        mBinding.vp.setAdapter(adapter);

        mBinding.tabLayout.setVisibility(View.GONE);
    }

    // 会员集采
    public void getCollectingType() {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showProgressDialog(this, "数据获取中...");
        }
        MemberConfig.getInstance().getApi().getIndustryList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    initTabLayout(data.getData());
                } else {
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                }
            }


            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initTabLayout(List<CommonPopEntity> data) {
        if (mFragments != null && mFragments.size() > 0) return;
        mBinding.emptyView.setVisibility(View.GONE);
        mFragments = new ArrayList<>();
        data.add(0, new CommonPopEntity("", "不限"));
        List<String> titles = new ArrayList<>();
        CommonPopEntity popEntity = data.get(0);
        mFragments.add(PromotionInfoFragment.newInstance(0, popEntity));
        SlidingFragmentPagerAdapter adapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles.toArray(new String[]{}));
        mBinding.vp.setOffscreenPageLimit(titles.size());
        mBinding.vp.setAdapter(adapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.vp);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBinding.tabLayout.setNormalColor(R.color.color_C0C4CC);
        mBinding.tabLayout.setSelectColor(R.color.color_white);
        mBinding.tabLayout.setNormalSize(15);
        mBinding.tabLayout.setSelectSize(18);
        mBinding.tabLayout.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.vp.setCurrentItem(tab.getPosition());
            }
        });
    }

    private String mSearchText = ""; // 搜索内容
    private final SparseArray<String> mSearchList = new SparseArray<>();

    private void initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String hint = "请输入关键词，比如商品名称、企业名称等";
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint(hint);
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        int tabPosition = mBinding.vp.getCurrentItem();
        return mSearchList.get(tabPosition);
    }

    private void loadSearchData() {
        int tabPosition = mBinding.vp.getCurrentItem();
        mSearchList.put(tabPosition, mSearchText);
        PromotionInfoFragment fragment = (PromotionInfoFragment) mFragments.get(tabPosition);
        fragment.loadData(false);
    }

}
